package com.nirvana.resolve;

import com.alibaba.excel.EasyExcel;
import com.nirvana.convert.CamelCaseConverter;
import com.nirvana.data.DefPropertyData;
import com.nirvana.resolve.listener.HeadDataListener;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.List;

public class ExcelResolver extends AbstractResolver {
    public static final int MAX_HEAD_ROW = 3;

    public ExcelResolver(File file) {
        super(file);
    }

    @Override
    public List<DefPropertyData> resolve() {
        // 字段名
        // 注释
        // 类型
        HeadDataListener headDataListener = new HeadDataListener();
        EasyExcel.read(file, headDataListener).sheet(0).headRowNumber(MAX_HEAD_ROW).doRead();
        return headDataListener.getData();
    }

    @Override
    public String getDefClassName() {
        try (InputStream inputStream = new FileInputStream(file)) {
            // 读取Excel文件
            Workbook workbook = new XSSFWorkbook(inputStream);

            String sheetName = workbook.getSheetName(0);
            return CamelCaseConverter.toCamelCase(sheetName, false) + "Def";

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
